package cn.xm.order.service.impl;

import cn.xm.basic.config.BussinessException;
import cn.xm.order.domain.OrderPetAcquisition;
import cn.xm.order.domain.OrderPetAcquisitionLog;
import cn.xm.order.mapper.OrderPetAcquisitionLogMapper;
import cn.xm.order.mapper.OrderPetAcquisitionMapper;
import cn.xm.order.service.OrderPetAcquisitionService;
import cn.xm.basic.service.impl.BaseServiceImpl;
import cn.xm.pet.domain.Pet;
import cn.xm.pet.domain.SearchMasterMsg;
import cn.xm.pet.dto.OrderConfirmDto;
import cn.xm.pet.mapper.PetMapper;
import cn.xm.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xm
 * @since 2022-11-01
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements OrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private OrderPetAcquisitionLogMapper orderPetAcquisitionLogMapper;
    @Autowired
    private PetMapper petMapper;
    @Override
    public void cancel(Long id) {
        if (id == null) {
            throw new BussinessException("不能传入空的订单id");
        }
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(id);
        if (orderPetAcquisition == null) {
            throw new BussinessException("不存在对应的订单");
        }
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.findById(orderPetAcquisition.getSearchMasterMsgId());
        searchMasterMsg.setState(-1);
        searchMasterMsg.setShopId(null);
        searchMasterMsgMapper.update(searchMasterMsg);
        // 记录日志
        OrderPetAcquisitionLog orderPetAcquisitionLog = new OrderPetAcquisitionLog();
        orderPetAcquisitionLog.setAcquisitionId(orderPetAcquisition.getId());
        orderPetAcquisitionLog.setAuditId(orderPetAcquisition.getEmpId());
        orderPetAcquisitionLogMapper.add(orderPetAcquisitionLog);


    }

    @Override
    public void confirm(OrderConfirmDto orderConfirmDto) {
        Long id = orderConfirmDto.getId();
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(id);
        // 修改订单
        orderPetAcquisition.setState(1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 修改寻主消息
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.findById(orderPetAcquisition.getSearchMasterMsgId());
        searchMasterMsg.setState(3);
        searchMasterMsgMapper.update(searchMasterMsg);
        // 生成宠物信息
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setShopId(searchMasterMsg.getShopId());
        pet.setCostprice(orderConfirmDto.getMoney());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        petMapper.add(pet);

    }
}
